/********************************difference-in-difference analysis of US-EP Twins********************************
Main purpose: compare forward cites, citation lag, and similarity for US-EP twins around AIPA
datasets:
us_ep_twin: US-EP twins, constructed based on data from PATSTAT 2017

********************************difference-in-difference analysis of US-EP Twins********************************/






use "$DATA\us_ep_twin",clear

************************Figure 7: Citations to US-EP "twins" before and after AIPA************************
//raw data plots with linear pre-trend estimated for US and EP patents, respectively

global chg=tm(2000m12) //add vertical line 491:=tm(2000m12)
preserve
keep if month>=tm(1998m1) & month<=tm(2003m12)
winsor2 fcite_pub3, by(uspto)
bys month uspto: egen mean=mean(fcite_pub3_w)
keep month uspto mean 
bys month uspto: keep if _n==1
sort uspto month 
by uspto: gen temp1=mean[1]
replace mean=mean/temp1
twoway (scatter mean month if uspto==1, ) ///
(scatter mean month if uspto==0,  msymbol(X) msize(medsmall)) /// ///
, xline(${chg}, lcolor(gray) lpattern("shortdash") lwidth(thick) ) /// 
 ytitle("Normalized 3-yr forward cites", size(medsmall)) xtitle("")  ylab(,nogrid) legend(off) ///
plotregion(fcolor(white)) graphregion(color(white)) bgcolor(white) 
graph export "$DATA\us_ep_fcite_pub3normal.eps", replace
restore

preserve
keep if month>=tm(1998m1) & month<=tm(2003m12)
winsor2 fcite_pub5, by(uspto)
bys month uspto: egen mean=mean(fcite_pub5_w)
keep month uspto mean 
bys month uspto: keep if _n==1
sort uspto month 
by uspto: gen temp1=mean[1]
replace mean=mean/temp1
twoway (scatter mean month if uspto==1, ) ///
(scatter mean month if uspto==0,  msymbol(X) msize(medsmall)) /// ///
, xline(${chg}, lcolor(gray) lpattern("shortdash") lwidth(thick) ) /// 
 ytitle("Normalized 5-yr forward cites", size(medsmall)) xtitle("")  ylab(,nogrid) legend(off) ///
plotregion(fcolor(white)) graphregion(color(white)) bgcolor(white) 
graph export "$DATA\us_ep_fcite_pub5normal.eps", replace
restore

preserve
keep if month>=tm(1998m1) & month<=tm(2003m12)
winsor2 fcite_pub7, by(uspto)
bys month uspto: egen mean=mean(fcite_pub7_w)
keep month uspto mean 
bys month uspto: keep if _n==1
sort uspto month 
by uspto: gen temp1=mean[1]
replace mean=mean/temp1
twoway (scatter mean month if uspto==1, ) ///
(scatter mean month if uspto==0,  msymbol(X) msize(medsmall)) /// ///
, xline(${chg}, lcolor(gray) lpattern("shortdash") lwidth(thick) ) /// 
 ytitle("Normalized 7-yr forward cites", size(medsmall)) xtitle("")  ylab(,nogrid) ///
 legend( region(color(white))  order(1 "US" 2 "EP") size(medsmall)) ///
plotregion(fcolor(white)) graphregion(color(white)) bgcolor(white) 
graph export "$DATA\us_ep_fcite_pub7normal.eps", replace
restore


preserve
keep if month>=tm(1998m1) & month<=tm(2003m12)
winsor2 fcite_pub10, by(uspto)
bys month uspto: egen mean=mean(fcite_pub10_w)
keep month uspto mean 
bys month uspto: keep if _n==1
sort uspto month 
by uspto: gen temp1=mean[1]
replace mean=mean/temp1
twoway (scatter mean month if uspto==1, ) ///
(scatter mean month if uspto==0,  msymbol(X) msize(medsmall)) /// ///
, xline(${chg}, lcolor(gray) lpattern("shortdash") lwidth(thick) ) /// 
 ytitle("Normalized 10-yr forward cites", size(medsmall)) xtitle("")  ylab(,nogrid) ///
 legend( region(color(white))  order(1 "US" 2 "EP") size(medsmall)) ///
plotregion(fcolor(white)) graphregion(color(white)) bgcolor(white) 
graph export "$DATA\us_ep_fcite_pub10normal.eps", replace
restore



************************Figure 8: Citation lags of US-EP "twins" before and after AIPA************************
foreach i of numlist 1 3{
preserve
keep if month>=tm(1998m1) & month<=tm(2003m12)
winsor2 lag`i'app
bys month uspto: egen mean=mean(lag`i'app_w)
keep month uspto mean 
bys month uspto: keep if _n==1
twoway (scatter mean month if uspto==1) ///
(scatter mean month if uspto==0,  msymbol(X) msize(medsmall)) ///
, xline(${chg}, lcolor(gray) lpattern("shortdash") lwidth(thick) ) /// 
ytitle("Months to `i' cite(s)", siz(med)) ylab(,nogrid) xtitle("") ///
legend(off) ///
plotregion(fcolor(white)) graphregion(color(white)) bgcolor(white) 
graph export "$DATA\us_ep_lag`i'raw.eps", replace
restore
}

foreach i of numlist 5 7{
preserve
keep if month>=tm(1998m1) & month<=tm(2003m12)
winsor2 lag`i'app
bys month uspto: egen mean=mean(lag`i'app_w)
keep month uspto mean 
bys month uspto: keep if _n==1
twoway (scatter mean month if uspto==1) ///
(scatter mean month if uspto==0,  msymbol(X) msize(medsmall)) ///
, xline(${chg}, lcolor(gray) lpattern("shortdash") lwidth(thick) ) /// 
ytitle("Months to `i' cite(s)", siz(med)) ylab(,nogrid) xtitle("") ///
legend( region(color(white)) c(2) order(1 "US" 2 "EP") size(medsmall)) ///
plotregion(fcolor(white)) graphregion(color(white)) bgcolor(white) 
graph export "$DATA\us_ep_lag`i'raw.eps", replace
restore
}




************************Figure 9: Similarity of US-EP "twins" before and after AIPA************************
foreach i of numlist 5 10 15 25 50 75{
preserve
capture rm us_ep_ipc7_`i'raw.gph
keep if month>=tm(1998m1) & month<=tm(2003m12)
winsor2 ipc7_`i'
bys month uspto: egen mean=mean(ipc7_`i'_w)
keep month uspto mean 
bys month uspto: keep if _n==1
twoway (scatter mean month if uspto==1) ///
(scatter mean month if uspto==0,  msymbol(X) msize(medsmall)) ///
, xline(${chg}, lcolor(gray) lpattern("shortdash") lwidth(thick) ) /// 
ytitle("Similarity `i'th Pctile", siz(med)) ylab(,nogrid) xtitle("") ///
legend(off) ///
plotregion(fcolor(white)) graphregion(color(white)) bgcolor(white) 
graph export "$DATA\us_ep_ipc7_`i'raw.eps", replace
restore
}


foreach i of numlist 85 90 95{
preserve
capture rm us_ep_ipc7_`i'raw.gph
keep if month>=tm(1998m1) & month<=tm(2003m12)
winsor2 ipc7_`i'
bys month uspto: egen mean=mean(ipc7_`i'_w)
keep month uspto mean 
bys month uspto: keep if _n==1
reg mean month if month<=tm(2000m11) & uspto==1 //US pre-trend
local us_cons=_b[_cons]
local us_month=_b[month]
reg mean month if month<=tm(2000m11) & uspto==0 //US pre-trend
local ep_cons=_b[_cons]
local ep_month=_b[month]
twoway (scatter mean month if uspto==1) ///
(scatter mean month if uspto==0,  msymbol(X) msize(medsmall)) ///
, xline(${chg}, lcolor(gray) lpattern("shortdash") lwidth(thick) ) /// 
ytitle("Similarity `i'th Pctile", siz(med)) ylab(,nogrid) xtitle("") ///
legend( region(color(white)) c(2) order(1 "US" 2 "EP") size(medsmall)) ///
plotregion(fcolor(white)) graphregion(color(white)) bgcolor(white) 
graph export "$DATA\us_ep_ipc7_`i'raw.eps", replace
restore
}












************************Table 4: AIPA's effect on forward citations************************
use "$DATA\us_ep_twin",clear

//Panel A
qui: areg lnfcite_pub3 i.uspto##i.post_aipa granted i.uspto##i.grant18m i.month, absorb(appln_id_us) vce(cluster month_country)
outreg2 using "$DATA\table4.xls", ctitle(fcite_pub3Y)  nocons bdec(3)  keep(1.uspto#1.post_aipa 1.uspto granted 1.grant18m 1.uspto#1.grant18m) adjr2 replace
qui: areg lnfcite_pub5 i.uspto##i.post_aipa granted i.uspto##i.grant18m i.month, absorb(appln_id_us) vce(cluster month_country)
outreg2 using "$DATA\table4.xls", ctitle(fcite_pub5Y)  nocons bdec(3)  keep(1.uspto#1.post_aipa 1.uspto granted 1.grant18m 1.uspto#1.grant18m) adjr2
qui: areg lnfcite_pub7 i.uspto##i.post_aipa granted i.uspto##i.grant18m i.month, absorb(appln_id_us) vce(cluster month_country)
outreg2 using "$DATA\table4.xls", ctitle(fcite_pub7Y)  nocons bdec(3)  keep(1.uspto#1.post_aipa 1.uspto granted 1.grant18m 1.uspto#1.grant18m) adjr2
qui: areg lnfcite_pub10 i.uspto##i.post_aipa granted i.uspto##i.grant18m i.month, absorb(appln_id_us) vce(cluster month_country)
outreg2 using "$DATA\table4.xls", ctitle(fcite_pub10Y)  nocons bdec(3)  keep(1.uspto#1.post_aipa 1.uspto granted 1.grant18m 1.uspto#1.grant18m) adjr2

//Panel B
qui: areg lnfcite_pub3us i.uspto##i.post_aipa granted i.uspto##i.grant18m i.month, absorb(appln_id_us) vce(cluster month_country)
outreg2 using "$DATA\table4.xls", ctitle(fcite_pub3Y_US)  nocons bdec(3)  keep(1.uspto#1.post_aipa 1.uspto granted 1.grant18m 1.uspto#1.grant18m) adjr2 
qui: areg lnfcite_pub5us i.uspto##i.post_aipa granted i.uspto##i.grant18m i.month, absorb(appln_id_us) vce(cluster month_country)
outreg2 using "$DATA\table4.xls", ctitle(fcite_pub5Y_US)  nocons bdec(3)  keep(1.uspto#1.post_aipa 1.uspto granted 1.grant18m 1.uspto#1.grant18m) adjr2
qui: areg lnfcite_pub7us i.uspto##i.post_aipa granted i.uspto##i.grant18m i.month, absorb(appln_id_us) vce(cluster month_country)
outreg2 using "$DATA\table4.xls", ctitle(fcite_pub7Y_US)  nocons bdec(3)  keep(1.uspto#1.post_aipa 1.uspto granted 1.grant18m 1.uspto#1.grant18m) adjr2
qui: areg lnfcite_pub10us i.uspto##i.post_aipa granted i.uspto##i.grant18m i.month, absorb(appln_id_us) vce(cluster month_country)
outreg2 using "$DATA\table4.xls", ctitle(fcite_pub10Y_US)  nocons bdec(3)  keep(1.uspto#1.post_aipa 1.uspto granted 1.grant18m 1.uspto#1.grant18m) adjr2

qui: areg lnfcite_pub3ep i.uspto##i.post_aipa granted i.uspto##i.grant18m i.month, absorb(appln_id_us) vce(cluster month_country)
outreg2 using "$DATA\table4.xls", ctitle(fcite_pub3Y_EP)  nocons bdec(3)  keep(1.uspto#1.post_aipa 1.uspto granted 1.grant18m 1.uspto#1.grant18m) adjr2 
qui: areg lnfcite_pub5ep i.uspto##i.post_aipa granted i.uspto##i.grant18m i.month, absorb(appln_id_us) vce(cluster month_country)
outreg2 using "$DATA\table4.xls", ctitle(fcite_pub5Y_EP)  nocons bdec(3)  keep(1.uspto#1.post_aipa 1.uspto granted 1.grant18m 1.uspto#1.grant18m) adjr2
qui: areg lnfcite_pub7ep i.uspto##i.post_aipa granted i.uspto##i.grant18m i.month, absorb(appln_id_us) vce(cluster month_country)
outreg2 using "$DATA\table4.xls", ctitle(fcite_pub7Y_EP)  nocons bdec(3)  keep(1.uspto#1.post_aipa 1.uspto granted 1.grant18m 1.uspto#1.grant18m) adjr2
qui: areg lnfcite_pub10ep i.uspto##i.post_aipa granted i.uspto##i.grant18m i.month, absorb(appln_id_us) vce(cluster month_country)
outreg2 using "$DATA\table4.xls", ctitle(fcite_pub10Y_EP)  nocons bdec(3)  keep(1.uspto#1.post_aipa 1.uspto granted 1.grant18m 1.uspto#1.grant18m) adjr2
rm "$DATA\table4.txt"





************************Table 5: AIPA's effect o citation lag************************
use "$DATA\us_ep_twin",clear
qui: areg lnlag1 i.uspto##i.post_aipa granted i.uspto##i.grant18m i.month, absorb(appln_id_us) vce(cluster month_country)
outreg2 using "$DATA\table5.xls", ctitle(Lag1)  nocons bdec(3)  keep(1.uspto#1.post_aipa 1.uspto granted 1.grant18m 1.uspto#1.grant18m) adjr2 replace
qui: areg lnlag3 i.uspto##i.post_aipa granted i.uspto##i.grant18m i.month, absorb(appln_id_us) vce(cluster month_country)
outreg2 using "$DATA\table5.xls", ctitle(Lag3)  nocons bdec(3)  keep(1.uspto#1.post_aipa 1.uspto granted 1.grant18m 1.uspto#1.grant18m) adjr2
qui: areg lnlag5 i.uspto##i.post_aipa granted i.uspto##i.grant18m i.month, absorb(appln_id_us) vce(cluster month_country)
outreg2 using "$DATA\table5.xls", ctitle(Lag5)  nocons bdec(3)  keep(1.uspto#1.post_aipa 1.uspto granted 1.grant18m 1.uspto#1.grant18m) adjr2
qui: areg lnlag7 i.uspto##i.post_aipa granted i.uspto##i.grant18m i.month, absorb(appln_id_us) vce(cluster month_country)
outreg2 using "$DATA\table5.xls", ctitle(Lag7)  nocons bdec(3)  keep(1.uspto#1.post_aipa 1.uspto granted 1.grant18m 1.uspto#1.grant18m) adjr2
rm "$DATA\table5.txt"





************************Table 6: AIPA's effect on technology similarity************************
use "$DATA\us_ep_twin",clear
qui: areg ipc7_50 i.uspto##i.post_aipa granted i.uspto##i.grant18m i.month, absorb(appln_id_us) vce(cluster month_country)
outreg2 using "$DATA\table6.xls", ctitle(sim50)  nocons bdec(3)  keep(1.uspto#1.post_aipa 1.uspto granted 1.grant18m 1.uspto#1.grant18m) adjr2  
qui: areg ipc7_75 i.uspto##i.post_aipa granted i.uspto##i.grant18m i.month, absorb(appln_id_us)  vce(cluster month_country)
outreg2 using "$DATA\table6.xls", ctitle(sim75)  nocons bdec(3)  keep(1.uspto#1.post_aipa 1.uspto granted 1.grant18m 1.uspto#1.grant18m) adjr2 
qui: areg ipc7_90 i.uspto##i.post_aipa granted i.uspto##i.grant18m i.month, absorb(appln_id_us)  vce(cluster month_country)
outreg2 using "$DATA\table6.xls", ctitle(sim90)  nocons bdec(3)  keep(1.uspto#1.post_aipa 1.uspto granted 1.grant18m 1.uspto#1.grant18m) adjr2
qui: areg ipc7_95 i.uspto##i.post_aipa granted i.uspto##i.grant18m i.month, absorb(appln_id_us)  vce(cluster month_country)
outreg2 using "$DATA\table6.xls", ctitle(sim95)  nocons bdec(3)  keep(1.uspto#1.post_aipa 1.uspto granted 1.grant18m 1.uspto#1.grant18m) adjr2
rm "$DATA\table6.txt"







************************Table 8 Panel A: PCT filings************************
use "$DATA\us_ep_twin",clear


qui: reghdfe lnfcite_pub3 i.uspto##i.post_aipa##i.pct granted i.uspto##i.grant18m, absorb(appln_id_us month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\table8_panelA.xls", ctitle(fcite_pub3Y)  nocons bdec(3)  keep(`r(X)') adjr2 replace
qui: reghdfe lnfcite_pub5 i.uspto##i.post_aipa##i.pct granted i.uspto##i.grant18m, absorb(appln_id_us month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\table8_panelA.xls", ctitle(fcite_pub5Y)  nocons bdec(3)  keep(`r(X)') adjr2
qui: reghdfe lnfcite_pub7 i.uspto##i.post_aipa##i.pct granted i.uspto##i.grant18m, absorb(appln_id_us month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\table8_panelA.xls", ctitle(fcite_pub7Y)  nocons bdec(3)  keep(`r(X)') adjr2
qui: reghdfe lnfcite_pub10 i.uspto##i.post_aipa##i.pct granted i.uspto##i.grant18m, absorb(appln_id_us month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\table8_panelA.xls", ctitle(fcite_pub10Y)  nocons bdec(3)  keep(`r(X)') adjr2


qui: reghdfe lnlag1 i.uspto##i.post_aipa##i.pct granted i.uspto##i.grant18m, absorb(appln_id_us month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\table8_panelA.xls", ctitle(Lag1)  nocons bdec(3)  keep(`r(X)') adjr2 
qui: reghdfe lnlag3 i.uspto##i.post_aipa##i.pct granted i.uspto##i.grant18m, absorb(appln_id_us month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\table8_panelA.xls", ctitle(Lag3)  nocons bdec(3)  keep(`r(X)') adjr2
qui: reghdfe lnlag5 i.uspto##i.post_aipa##i.pct granted i.uspto##i.grant18m, absorb(appln_id_us month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\table8_panelA.xls", ctitle(Lag5)  nocons bdec(3)  keep(`r(X)') adjr2
qui: reghdfe lnlag7 i.uspto##i.post_aipa##i.pct granted i.uspto##i.grant18m, absorb(appln_id_us month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\table8_panelA.xls", ctitle(Lag7)  nocons bdec(3)  keep(`r(X)') adjr2
rm "$DATA\table8_panelA.txt"






************************Table 8 Panel B: foreign investors************************
use "$DATA\us_ep_twin",clear

*citation count
qui: reghdfe lnfcite_pub3 i.uspto##i.post_aipa##i.for_inventor granted i.uspto##i.grant18m, absorb(appln_id_us month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\table8_panelB.xls", ctitle(fcite_pub3Y)  nocons bdec(3)  keep(`r(X)') adjr2 replace
qui: reghdfe lnfcite_pub5 i.uspto##i.post_aipa##i.for_inventor granted i.uspto##i.grant18m, absorb(appln_id_us month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\table8_panelB.xls", ctitle(fcite_pub5Y)  nocons bdec(3)  keep(`r(X)') adjr2
qui: reghdfe lnfcite_pub7 i.uspto##i.post_aipa##i.for_inventor granted i.uspto##i.grant18m, absorb(appln_id_us month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\table8_panelB.xls", ctitle(fcite_pub7Y)  nocons bdec(3)  keep(`r(X)') adjr2
qui: reghdfe lnfcite_pub10 i.uspto##i.post_aipa##i.for_inventor granted i.uspto##i.grant18m, absorb(appln_id_us month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\table8_panelB.xls", ctitle(fcite_pub10Y)  nocons bdec(3)  keep(`r(X)') adjr2


*citation lag
qui: reghdfe lnlag1 i.uspto##i.post_aipa##i.for_inventor granted i.uspto##i.grant18m, absorb(appln_id_us month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\table8_panelB.xls", ctitle(Lag1)  nocons bdec(3)  keep(`r(X)') adjr2 
qui: reghdfe lnlag3 i.uspto##i.post_aipa##i.for_inventor granted i.uspto##i.grant18m, absorb(appln_id_us month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\table8_panelB.xls", ctitle(Lag3)  nocons bdec(3)  keep(`r(X)') adjr2
qui: reghdfe lnlag5 i.uspto##i.post_aipa##i.for_inventor granted i.uspto##i.grant18m, absorb(appln_id_us month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\table8_panelB.xls", ctitle(Lag5)  nocons bdec(3)  keep(`r(X)') adjr2
qui: reghdfe lnlag7 i.uspto##i.post_aipa##i.for_inventor granted i.uspto##i.grant18m, absorb(appln_id_us month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\table8_panelB.xls", ctitle(Lag7)  nocons bdec(3)  keep(`r(X)') adjr2
rm "$DATA\table8_panelB.txt"




************************Table 8 Panel A: Pre-AIPA Grant Delays************************
use "$DATA\us_ep_twin",clear

qui: reghdfe lnfcite_pub3 i.uspto##i.post_aipa##c.gap3_us granted i.uspto##i.grant18m, absorb(appln_id_us month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\table9_panelA.xls", ctitle(fcite_pub3Y)  nocons bdec(3) adjr2 keep(`r(X)') replace
qui: reghdfe lnfcite_pub5 i.uspto##i.post_aipa##c.gap3_us granted i.uspto##i.grant18m, absorb(appln_id_us month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\table9_panelA.xls", ctitle(fcite_pub5Y)  nocons bdec(3) adjr2 keep(`r(X)')
qui: reghdfe lnfcite_pub7 i.uspto##i.post_aipa##c.gap3_us granted i.uspto##i.grant18m, absorb(appln_id_us month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\table9_panelA.xls", ctitle(fcite_pub7Y)  nocons bdec(3)  adjr2 keep(`r(X)')
qui: reghdfe lnfcite_pub10 i.uspto##i.post_aipa##c.gap3_us granted i.uspto##i.grant18m, absorb(appln_id_us month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\table9_panelA.xls", ctitle(fcite_pub10Y)  nocons bdec(3) adjr2 keep(`r(X)')


qui: reghdfe lnlag1 i.uspto##i.post_aipa##c.gap3_us granted i.uspto##i.grant18m, absorb(appln_id_us month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\table9_panelA.xls", ctitle(Lag1)  nocons bdec(3)  keep(`r(X)') adjr2 
qui: reghdfe lnlag3 i.uspto##i.post_aipa##c.gap3_u granted i.uspto##i.grant18m, absorb(appln_id_us month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\table9_panelA.xls", ctitle(Lag3)  nocons bdec(3)  keep(`r(X)') adjr2
qui: reghdfe lnlag5 i.uspto##i.post_aipa##c.gap3_u granted i.uspto##i.grant18m, absorb(appln_id_us month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\table9_panelA.xls", ctitle(Lag5)  nocons bdec(3)  keep(`r(X)') adjr2
qui: reghdfe lnlag7 i.uspto##i.post_aipa##c.gap3_u granted i.uspto##i.grant18m, absorb(appln_id_us month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\table9_panelA.xls", ctitle(Lag7)  nocons bdec(3)  keep(`r(X)') adjr2
rm "$DATA\table9_panelA.txt"



************************Table 9 Panel B: Post-AIPA opt-out rate************************
use "$DATA\us_ep_twin",clear

qui: reghdfe lnfcite_pub3 i.uspto##i.post_aipa##c.optout3ipcmain granted i.uspto##i.grant18m, absorb(appln_id_us month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\table9_panelB.xls", ctitle(fcite_pub3Y)  nocons bdec(3)  keep(`r(X)') adjr2 replace
qui: reghdfe lnfcite_pub5 i.uspto##i.post_aipa##c.optout3ipcmain granted i.uspto##i.grant18m, absorb(appln_id_us month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\table9_panelB.xls", ctitle(fcite_pub5Y)  nocons bdec(3)  keep(`r(X)') adjr2
qui: reghdfe lnfcite_pub7 i.uspto##i.post_aipa##c.optout3ipcmain granted i.uspto##i.grant18m, absorb(appln_id_us month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\table9_panelB.xls", ctitle(fcite_pub7Y)  nocons bdec(3)  keep(`r(X)') adjr2
qui: reghdfe lnfcite_pub10 i.uspto##i.post_aipa##c.optout3ipcmain granted i.uspto##i.grant18m, absorb(appln_id_us month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\table9_panelB.xls", ctitle(fcite_pub10Y)  nocons bdec(3)  keep(`r(X)') adjr2


qui: reghdfe lnlag1 i.uspto##i.post_aipa##c.optout3ipcmain granted i.uspto##i.grant18m, absorb(appln_id_us month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\table9_panelB.xls", ctitle(Lag1)  nocons bdec(3)  keep(`r(X)') adjr2 
qui: reghdfe lnlag3 i.uspto##i.post_aipa##c.optout3ipcmain granted i.uspto##i.grant18m, absorb(appln_id_us month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\table9_panelB.xls", ctitle(Lag3)  nocons bdec(3)  keep(`r(X)') adjr2
qui: reghdfe lnlag5 i.uspto##i.post_aipa##c.optout3ipcmain granted i.uspto##i.grant18m, absorb(appln_id_us month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\table9_panelB.xls", ctitle(Lag5)  nocons bdec(3)  keep(`r(X)') adjr2
qui: reghdfe lnlag7 i.uspto##i.post_aipa##c.optout3ipcmain granted i.uspto##i.grant18m, absorb(appln_id_us month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\table9_panelB.xls", ctitle(Lag7)  nocons bdec(3)  keep(`r(X)') adjr2
rm "$DATA\table9_panelB.txt"








***********************************Table 10***********************************
use "$DATA\us_ep_twin",clear

//level
qui: reghdfe lnsf_pub3 i.uspto##i.post_aipa granted i.uspto##i.grant18m , absorb(appln_id_us month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\table10.xls", ctitle(SF_pub3Y)  nocons bdec(3)  keep(`r(X)') adjr2 replace
qui: reghdfe lnsf_pub5 i.uspto##i.post_aipa granted i.uspto##i.grant18m , absorb(appln_id_us month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\table10.xls", ctitle(SF_pub5Y)  nocons bdec(3)  keep(`r(X)') adjr2
qui: reghdfe lnsf_pub7 i.uspto##i.post_aipa granted i.uspto##i.grant18m , absorb(appln_id_us month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\table10.xls", ctitle(SF_pub7Y)  nocons bdec(3)  keep(`r(X)') adjr2
qui: reghdfe lnsf_pub10 i.uspto##i.post_aipa granted i.uspto##i.grant18m , absorb(appln_id_us month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\table10.xls", ctitle(SF_pub10Y)  nocons bdec(3)  keep(`r(X)') adjr2

//%
qui: reghdfe sf_pub3 i.uspto##i.post_aipa granted i.uspto##i.grant18m, absorb(appln_id_us month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\table10.xls", ctitle(SF%pub3Y)  nocons bdec(3)  keep(`r(X)') adjr2 
qui: reghdfe sf_pub5 i.uspto##i.post_aipa granted i.uspto##i.grant18m, absorb(appln_id_us month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\table10.xls", ctitle(SF%pub5Y)  nocons bdec(3)  keep(`r(X)') adjr2
qui: reghdfe sf_pub7 i.uspto##i.post_aipa granted i.uspto##i.grant18m, absorb(appln_id_us month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\table10.xls", ctitle(SF%pub7Y)  nocons bdec(3)  keep(`r(X)') adjr2
qui: reghdfe sf_pub10 i.uspto##i.post_aipa granted i.uspto##i.grant18m, absorb(appln_id_us month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\table10.xls", ctitle(SF%pub10Y)  nocons bdec(3)  keep(`r(X)') adjr2
rm  "$DATA\table10.txt"








***********************************Table  D2: alternative fixed effect specification***********************************
**************************************Panel A: technology class fixed effects**************************************
use "$DATA\us_ep_twin",clear

qui: reghdfe lnfcite_pub3 i.uspto##i.post_aipa granted i.uspto##i.grant18m, absorb(ipcmain month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\tabled2_panelA.xls", ctitle(fcite_pub3Y)  nocons bdec(3)  keep(`r(X)') adjr2 replace
qui: reghdfe lnfcite_pub5 i.uspto##i.post_aipa granted i.uspto##i.grant18m, absorb(ipcmain month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\tabled2_panelA.xls", ctitle(fcite_pub5Y)  nocons bdec(3)  keep(`r(X)') adjr2
qui: reghdfe lnfcite_pub7 i.uspto##i.post_aipa granted i.uspto##i.grant18m, absorb(ipcmain month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\tabled2_panelA.xls", ctitle(fcite_pub7Y)  nocons bdec(3)  keep(`r(X)') adjr2
qui: reghdfe lnfcite_pub10 i.uspto##i.post_aipa granted i.uspto##i.grant18m, absorb(ipcmain month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\tabled2_panelA.xls", ctitle(fcite_pub10Y)  nocons bdec(3)  keep(`r(X)') adjr2

qui: reghdfe lnlag1 i.uspto##i.post_aipa granted i.uspto##i.grant18m, absorb(ipcmain month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\tabled2_panelA.xls",  nocons bdec(3)  keep(`r(X)') adjr2 
qui: reghdfe lnlag3 i.uspto##i.post_aipa granted i.uspto##i.grant18m, absorb(ipcmain month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\tabled2_panelA.xls",  nocons bdec(3)  keep(`r(X)') adjr2
qui: reghdfe lnlag5 i.uspto##i.post_aipa granted i.uspto##i.grant18m, absorb(ipcmain month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\tabled2_panelA.xls", nocons bdec(3)  keep(`r(X)') adjr2
qui: reghdfe lnlag7 i.uspto##i.post_aipa granted i.uspto##i.grant18m, absorb(ipcmain month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\tabled2_panelA.xls", nocons bdec(3)  keep(`r(X)') adjr2

qui: reghdfe ipc7_50 i.uspto##i.post_aipa granted i.uspto##i.grant18m, absorb(ipcmain month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\tabled2_panelA.xls",  nocons bdec(3)  keep(`r(X)') adjr2 
qui: reghdfe ipc7_75 i.uspto##i.post_aipa granted i.uspto##i.grant18m, absorb(ipcmain month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\tabled2_panelA.xls",  nocons bdec(3)  keep(`r(X)') adjr2
qui: reghdfe ipc7_90 i.uspto##i.post_aipa granted i.uspto##i.grant18m, absorb(ipcmain month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\tabled2_panelA.xls",  nocons bdec(3)  keep(`r(X)') adjr2
qui: reghdfe ipc7_95 i.uspto##i.post_aipa granted i.uspto##i.grant18m, absorb(ipcmain month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\tabled2_panelA.xls", nocons bdec(3)  keep(`r(X)') adjr2
rm "$DATA\tabled2_panelA.txt"




**************************************PanelB: applicant fixed effects (US & EP based on tls206 and tls217)**************************************
use "$DATA\us_ep_twin",clear

qui: reghdfe lnfcite_pub3 i.uspto##i.post_aipa granted i.uspto##i.grant18m, absorb(psn_id1 month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\tabled2_panelB.xls", ctitle(fcite_pub3Y)  nocons bdec(3)  keep(`r(X)') adjr2 replace
qui: reghdfe lnfcite_pub5 i.uspto##i.post_aipa granted i.uspto##i.grant18m, absorb(psn_id1 month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\tabled2_panelB.xls", ctitle(fcite_pub5Y)  nocons bdec(3)  keep(`r(X)') adjr2
qui: reghdfe lnfcite_pub7 i.uspto##i.post_aipa granted i.uspto##i.grant18m, absorb(psn_id1 month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\tabled2_panelB.xls", ctitle(fcite_pub7Y)  nocons bdec(3)  keep(`r(X)') adjr2
qui: reghdfe lnfcite_pub10 i.uspto##i.post_aipa granted i.uspto##i.grant18m, absorb(psn_id1 month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\tabled2_panelB.xls", ctitle(fcite_pub10Y)  nocons bdec(3)  keep(`r(X)') adjr2


qui: reghdfe lnlag1 i.uspto##i.post_aipa granted i.uspto##i.grant18m, absorb(psn_id1 month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\tabled2_panelB.xls", ctitle(Lag1)  nocons bdec(3)  keep(`r(X)') adjr2 
qui: reghdfe lnlag3 i.uspto##i.post_aipa granted i.uspto##i.grant18m, absorb(psn_id1 month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\tabled2_panelB.xls", ctitle(Lag3)  nocons bdec(3)  keep(`r(X)') adjr2
qui: reghdfe lnlag5 i.uspto##i.post_aipa granted i.uspto##i.grant18m, absorb(psn_id1 month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\tabled2_panelB.xls", ctitle(Lag5)  nocons bdec(3)  keep(`r(X)') adjr2
qui: reghdfe lnlag7 i.uspto##i.post_aipa granted i.uspto##i.grant18m, absorb(psn_id1 month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\tabled2_panelB.xls", ctitle(Lag7)  nocons bdec(3)  keep(`r(X)') adjr2


foreach i of numlist 50 75 90 95{
qui: reghdfe ipc7_`i' i.uspto##i.post_aipa granted i.uspto##i.grant18m, absorb(psn_id1 month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\tabled2_panelB.xls", ctitle(Sim`i')  nocons bdec(3) keep(`r(X)') adjr2
}
rm "$DATA\tabled2_panelB.txt"



**************************************Table D3: Country-technology specific time trend**************************************
use "$DATA\us_ep_twin",clear

gen ipc1=substr(ipcmain,1,1)
local ipc1list "A B C D E F G H"
foreach i of local ipc1list{
gen month_X_us_X_`i'=month_X_us if ipc1=="`i'" //month_X_us =calendar month for US patents; 0 otherwise
replace month_X_us_X_`i'=0 if month_X_us_X_`i'==. & !missing(ipc1)
gen month_X_ep_X_`i'=month_X_ep if ipc1=="`i'" //month_X_ep =calendar month for EP patents; 0 otherwise
replace month_X_ep_X_`i'=0 if month_X_ep_X_`i'==. & !missing(ipc1)
}

qui: reghdfe lnfcite_pub3 i.uspto##i.post_aipa granted i.uspto##i.grant18m month_X_us_* month_X_ep_*, absorb(appln_id_us month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\tabled3.xls", ctitle(fcite_pub3Y)  nocons bdec(3)  keep(`r(X)') adjr2 replace
qui: reghdfe lnfcite_pub5 i.uspto##i.post_aipa granted i.uspto##i.grant18m month_X_us_* month_X_ep_*, absorb(appln_id_us month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\tabled3.xls", ctitle(fcite_pub5Y)  nocons bdec(3)  keep(`r(X)') adjr2
qui: reghdfe lnfcite_pub7 i.uspto##i.post_aipa granted i.uspto##i.grant18m month_X_us_* month_X_ep_*, absorb(appln_id_us month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\tabled3.xls", ctitle(fcite_pub7Y)  nocons bdec(3)  keep(`r(X)') adjr2
qui: reghdfe lnfcite_pub10 i.uspto##i.post_aipa granted i.uspto##i.grant18m month_X_us_* month_X_ep_*, absorb(appln_id_us month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\tabled3.xls", ctitle(fcite_pub10Y)  nocons bdec(3)  keep(`r(X)') adjr2


qui: reghdfe lnlag1 i.uspto##i.post_aipa granted i.uspto##i.grant18m month_X_us_* month_X_ep_*, absorb(appln_id_us month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\tabled3.xls", nocons bdec(3)  keep(`r(X)') adjr2 
qui: reghdfe lnlag3 i.uspto##i.post_aipa granted i.uspto##i.grant18m month_X_us_* month_X_ep_*, absorb(appln_id_us month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\tabled3.xls", nocons bdec(3)  keep(`r(X)') adjr2
qui: reghdfe lnlag5 i.uspto##i.post_aipa granted i.uspto##i.grant18m month_X_us_* month_X_ep_*, absorb(appln_id_us month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\tabled3.xls", nocons bdec(3)  keep(`r(X)') adjr2
qui: reghdfe lnlag7 i.uspto##i.post_aipa granted i.uspto##i.grant18m month_X_us_* month_X_ep_*, absorb(appln_id_us month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\tabled3.xls",  nocons bdec(3)  keep(`r(X)') adjr2


qui: reghdfe ipc7_50 i.uspto##i.post_aipa granted i.uspto##i.grant18m month_X_us_* month_X_ep_*, absorb(appln_id_us month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\tabled3.xls", nocons bdec(3)  keep(`r(X)') adjr2 
qui: reghdfe ipc7_75 i.uspto##i.post_aipa granted i.uspto##i.grant18m month_X_us_* month_X_ep_*, absorb(appln_id_us month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\tabled3.xls", nocons bdec(3)  keep(`r(X)') adjr2
qui: reghdfe ipc7_90 i.uspto##i.post_aipa granted i.uspto##i.grant18m month_X_us_* month_X_ep_*, absorb(appln_id_us month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\tabled3.xls", nocons bdec(3)  keep(`r(X)') adjr2
qui: reghdfe ipc7_95 i.uspto##i.post_aipa granted i.uspto##i.grant18m month_X_us_* month_X_ep_*, absorb(appln_id_us month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\tabled3.xls",  nocons bdec(3)  keep(`r(X)') adjr2
rm "$DATA\tabled3.txt"




***************************Table D4: DID analysis with linear pre-trends***************************
****define function lincom_window to test the significance of AIPA impact in a specificied window [twin sample]****
capture program drop lincom_window
program define lincom_window
	if `1'==12{
	local len=502
	}
	else if `1'==24{
	local len=514
	}
	else if `1'==24{
	local len=526
	}
	else{
	local len=527
	}
	local lincom="_b[1.uspto#491.monthdum]"
	forvalues i=492/`len'{ //`1' is the first input from the function of lincom_window
	local lincom="`lincom'"+"+_b[1.uspto#`i'.monthdum]"
	}
	lincom 1/`1'*(`lincom')
end
****define function lincom_window to test the significance of AIPA impact in a specificied window [twin sample]****

****define function to test the significance of AIPA impact for pre-post comparison****
capture program drop lincom_prepost
program define lincom_prepost
	if `1'==12{
	local len=502
	}
	else if `1'==24{
	local len=514
	}
	else if `1'==18{
	local len=508
	}
	else if `1'==36{
	local len=526
	}
	else{
	local len=527
	}
	local lincom="_b[491.monthdum]"
	forvalues i=492/`len'{ //`1' is the first input from the function of lincom_window
	local lincom="`lincom'"+"+_b[`i'.monthdum]"
	}
	lincom 1/`1'*(`lincom')
end



use "$DATA\us_ep_twin",clear
log using "$DATA\tabled4_log.txt", replace
qui: reg lnfcite_pub3 month i.uspto##i.monthdum granted i.uspto##i.grant18m, vce(cluster month_country)
indeplist
outreg2 using "$DATA\tabled4.xls", ctitle(fcite_pub3Y)  nocons bdec(3)  keep(`r(X)') adjr2 replace 
lincom_prepost 37
lincom_window 37
qui: reg lnfcite_pub5 month i.uspto##i.monthdum granted i.uspto##i.grant18m, vce(cluster month_country)
indeplist
outreg2 using "$DATA\tabled4.xls", ctitle(fcite_pub5Y)  nocons bdec(3)  keep(`r(X)') adjr2 
lincom_prepost 37
lincom_window 37
qui: reg lnfcite_pub7 month i.uspto##i.monthdum granted i.uspto##i.grant18m, vce(cluster month_country)
indeplist
outreg2 using "$DATA\tabled4.xls", ctitle(fcite_pub7Y)  nocons bdec(3)  keep(`r(X)') adjr2
lincom_prepost 37
lincom_window 37
qui: reg lnfcite_pub10 month i.uspto##i.monthdum granted i.uspto##i.grant18m, vce(cluster month_country)
indeplist
outreg2 using "$DATA\tabled4.xls", ctitle(fcite_pub10Y)  nocons bdec(3)  keep(`r(X)') adjr2
lincom_prepost 37
lincom_window 37

qui: reg lnlag1 month i.uspto##i.monthdum granted i.uspto##i.grant18m, vce(cluster month_country)
indeplist
outreg2 using "$DATA\tabled4.xls", ctitle(Lag1)  nocons bdec(3)  keep(`r(X)') adjr2 
lincom_prepost 37
lincom_window 37
qui: reg lnlag3 month i.uspto##i.monthdum granted i.uspto##i.grant18m, vce(cluster month_country)
indeplist
outreg2 using "$DATA\tabled4.xls", ctitle(Lag3)  nocons bdec(3)  keep(`r(X)') adjr2
lincom_prepost 37
lincom_window 37
qui: reg lnlag5 month i.uspto##i.monthdum granted i.uspto##i.grant18m, vce(cluster month_country)
indeplist
outreg2 using "$DATA\tabled4.xls", ctitle(Lag5)  nocons bdec(3)  keep(`r(X)') adjr2
lincom_prepost 37
lincom_window 37
qui: reg lnlag7 month i.uspto##i.monthdum granted i.uspto##i.grant18m, vce(cluster month_country)
indeplist
outreg2 using "$DATA\tabled4.xls", ctitle(Lag7)  nocons bdec(3)  keep(`r(X)') adjr2
lincom_prepost 37
lincom_window 37


foreach i in  50 75 90 95{
qui: reg ipc7_`i' month i.uspto##i.monthdum granted i.uspto##i.grant18m, vce(cluster month_country)
indeplist
outreg2 using "$DATA\tabled4.xls", ctitle(Sim`i')  nocons dec(3)  keep(`r(X)') adjr2
lincom_prepost 37
lincom_window 37
}
rm  "$DATA\tabled4.txt"
log close



***************************Table D5: excluding the year prior to AIPA effective date***************************
use "$DATA\us_ep_twin",clear
preserve 
drop if appln_filing_date>=td(29nov1999) & appln_filing_date<=td(28nov2000) 

qui: reghdfe lnfcite_pub3 i.uspto##i.post_aipa granted i.uspto##i.grant18m, absorb(appln_id_us month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\tabled5.xls", ctitle(fcite_pub3Y)  nocons bdec(3)  keep(`r(X)') adjr2 replace
qui: reghdfe lnfcite_pub5 i.uspto##i.post_aipa granted i.uspto##i.grant18m, absorb(appln_id_us month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\tabled5.xls", ctitle(fcite_pub5Y)  nocons bdec(3)  keep(`r(X)') adjr2
qui: reghdfe lnfcite_pub7 i.uspto##i.post_aipa granted i.uspto##i.grant18m, absorb(appln_id_us month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\tabled5.xls", ctitle(fcite_pub7Y)  nocons bdec(3)  keep(`r(X)') adjr2
qui: reghdfe lnfcite_pub10 i.uspto##i.post_aipa granted i.uspto##i.grant18m, absorb(appln_id_us month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\tabled5.xls", ctitle(fcite_pub10Y)  nocons bdec(3)  keep(`r(X)') adjr2

qui: reghdfe lnlag1 i.uspto##i.post_aipa granted i.uspto##i.grant18m, absorb(appln_id_us month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\tabled5.xls", ctitle(Lag1)  nocons bdec(3)  keep(`r(X)') adjr2 
qui: reghdfe lnlag3 i.uspto##i.post_aipa granted i.uspto##i.grant18m, absorb(appln_id_us month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\tabled5.xls", ctitle(Lag3)  nocons bdec(3)  keep(`r(X)') adjr2
qui: reghdfe lnlag5 i.uspto##i.post_aipa granted i.uspto##i.grant18m, absorb(appln_id_us month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\tabled5.xls", ctitle(Lag5)  nocons bdec(3)  keep(`r(X)') adjr2
qui: reghdfe lnlag7 i.uspto##i.post_aipa granted i.uspto##i.grant18m, absorb(appln_id_us month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\tabled5.xls", ctitle(Lag7)  nocons bdec(3)  keep(`r(X)') adjr2


foreach i in  50 75 90 95{
qui: reghdfe ipc7_`i' i.uspto##i.post_aipa granted i.uspto##i.grant18m, absorb(appln_id_us month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\tabled5.xls", ctitle(Sim`i')  nocons bdec(3) keep(`r(X)') adjr2
}
rm "$DATA\tabled5.txt"
restore 




************************************************Table D6: placebo AIPA date************************************************
use "$DATA\us_ep_twin",clear
keep if appdate_us<td(29nov2000) //keep pre-AIPA period
replace post_aipa=appln_filing_date>=td(1jul1999) //reset the post dummy for placebo AIPA data

qui: reghdfe lnfcite_pub3 i.uspto##i.post_aipa granted i.uspto##i.grant18m, absorb(appln_id_us month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\tabled6.xls", ctitle(fcite_pub3Y)  nocons bdec(3)  keep(`r(X)') adjr2 replace
qui: reghdfe lnfcite_pub5 i.uspto##i.post_aipa granted i.uspto##i.grant18m, absorb(appln_id_us month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\tabled6.xls", ctitle(fcite_pub5Y)  nocons bdec(3)  keep(`r(X)') adjr2
qui: reghdfe lnfcite_pub7 i.uspto##i.post_aipa granted i.uspto##i.grant18m, absorb(appln_id_us month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\tabled6.xls", ctitle(fcite_pub7Y)  nocons bdec(3)  keep(`r(X)') adjr2
qui: reghdfe lnfcite_pub10 i.uspto##i.post_aipa granted i.uspto##i.grant18m, absorb(appln_id_us month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\tabled6.xls", ctitle(fcite_pub10Y)  nocons bdec(3)  keep(`r(X)') adjr2


qui: reghdfe lnlag1 i.uspto##i.post_aipa granted i.uspto##i.grant18m, absorb(appln_id_us month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\tabled6.xls", ctitle(Lag1)  nocons bdec(3)  keep(`r(X)') adjr2 
qui: reghdfe lnlag3 i.uspto##i.post_aipa granted i.uspto##i.grant18m, absorb(appln_id_us month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\tabled6.xls", ctitle(Lag3)  nocons bdec(3)  keep(`r(X)') adjr2
qui: reghdfe lnlag5 i.uspto##i.post_aipa granted i.uspto##i.grant18m, absorb(appln_id_us month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\tabled6.xls", ctitle(Lag5)  nocons bdec(3)  keep(`r(X)') adjr2
qui: reghdfe lnlag7 i.uspto##i.post_aipa granted i.uspto##i.grant18m, absorb(appln_id_us month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\tabled6.xls", ctitle(Lag7)  nocons bdec(3)  keep(`r(X)') adjr2


foreach i of numlist 50 75 90 95{
qui: reghdfe ipc7_`i' i.uspto##i.post_aipa granted i.uspto##i.grant18m, absorb(appln_id_us month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\tabled6.xls", ctitle(Sim`i') keep(`r(X)') nocons dec(3) adjr2
}
rm "$DATA\tabled6.txt"






************************************************Table D7: restricted to US patents granted within 18 months************************************************
use "$DATA\us_ep_twin",clear

*~~~~restricted to US patents granted within 18 months~~~~~*
gen grant18m_us=grant18 if uspto==1 
sort appln_id_us grant18m_us 
by appln_id_us: replace grant18m_us=grant18m_us[_n-1] if grant18m_us==.
keep if grant18m_us==1 


qui: reghdfe lnfcite_pub3 i.uspto##i.post_aipa granted grant18m, absorb(appln_id_us month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\tabled7.xls", ctitle(fcite_pub3Y)  nocons bdec(3)  keep(`r(X)') adjr2 replace
qui: reghdfe lnfcite_pub5 i.uspto##i.post_aipa granted grant18m, absorb(appln_id_us month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\tabled7.xls", ctitle(fcite_pub5Y)  nocons bdec(3)  keep(`r(X)') adjr2
qui: reghdfe lnfcite_pub7 i.uspto##i.post_aipa granted grant18m, absorb(appln_id_us month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\tabled7.xls", ctitle(fcite_pub7Y)  nocons bdec(3)  keep(`r(X)') adjr2
qui: reghdfe lnfcite_pub10 i.uspto##i.post_aipa granted grant18m, absorb(appln_id_us month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\tabled7.xls", ctitle(fcite_pub10Y)  nocons bdec(3)  keep(`r(X)') adjr2



qui: reghdfe lnlag1 i.uspto##i.post_aipa granted grant18m, absorb(appln_id_us month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\tabled7.xls", ctitle(Lag1)  nocons bdec(3)  keep(`r(X)') adjr2 
qui: reghdfe lnlag3 i.uspto##i.post_aipa granted grant18m, absorb(appln_id_us month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\tabled7.xls", ctitle(Lag3)  nocons bdec(3)  keep(`r(X)') adjr2
qui: reghdfe lnlag5 i.uspto##i.post_aipa granted grant18m, absorb(appln_id_us month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\tabled7.xls", ctitle(Lag5)  nocons bdec(3)  keep(`r(X)') adjr2
qui: reghdfe lnlag7 i.uspto##i.post_aipa granted grant18m, absorb(appln_id_us month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\tabled7.xls", ctitle(Lag7)  nocons bdec(3)  keep(`r(X)') adjr2
rm "$DATA\tabled7.txt"






************************************************Table E1: citation lag computed using priority dates************************************************
use "$DATA\us_ep_twin",clear

gen lnlag1pri=ln(lag_pri2pri1+1)
gen lnlag3pri=ln(lag_pri2pri3+1)
gen lnlag5pri=ln(lag_pri2pri5+1)
gen lnlag7pri=ln(lag_pri2pri7+1)


qui: reghdfe lnlag1pri i.uspto##i.post_aipa granted i.uspto##i.grant18m, absorb(appln_id_us month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\tablee1.xls", ctitle(Lag1)  nocons bdec(3)  keep(`r(X)') adjr2  replace
qui: reghdfe lnlag3pri i.uspto##i.post_aipa granted i.uspto##i.grant18m, absorb(appln_id_us month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\tablee1.xls", ctitle(Lag3)  nocons bdec(3)  keep(`r(X)') adjr2 
qui: reghdfe lnlag5pri i.uspto##i.post_aipa granted i.uspto##i.grant18m, absorb(appln_id_us month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\tablee1.xls", ctitle(Lag5)  nocons bdec(3)  keep(`r(X)') adjr2 
qui: reghdfe lnlag7pri i.uspto##i.post_aipa granted i.uspto##i.grant18m, absorb(appln_id_us month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\tablee1.xls", ctitle(Lag7)  nocons bdec(3)  keep(`r(X)') adjr2 
rm "$DATA\tablee1.txt"




************************************************Table E2: excluding continuation filings************************************************
use "$DATA\us_ep_twin",clear

preserve 
*~~~~excluding continuation filings~~~~~*
egen con3=rowmax(cip con div) 
//cip=1 if contn_type=="CIP" (continuation in part) based on table tls216 from PATSTAT
//con=1 if contn_type=="CON" (continuation) based on table tls216 from PATSTAT
//con=1 if contn_type=="DIV" (divisional) based on table tls216 from PATSTAT
drop if con3==1
qui: reghdfe lnfcite_pub3 i.uspto##i.post_aipa granted i.uspto##i.grant18m, absorb(appln_id_us month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\tablee2.xls", ctitle(fcite_pub3Y)  nocons bdec(3)  keep(`r(X)') adjr2 replace
qui: reghdfe lnfcite_pub5 i.uspto##i.post_aipa granted i.uspto##i.grant18m, absorb(appln_id_us month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\tablee2.xls", ctitle(fcite_pub5Y)  nocons bdec(3)  keep(`r(X)') adjr2
qui: reghdfe lnfcite_pub7 i.uspto##i.post_aipa granted i.uspto##i.grant18m, absorb(appln_id_us month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\tablee2.xls", ctitle(fcite_pub7Y)  nocons bdec(3)  keep(`r(X)') adjr2
qui: reghdfe lnfcite_pub10 i.uspto##i.post_aipa granted i.uspto##i.grant18m, absorb(appln_id_us month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\tablee2.xls", ctitle(fcite_pub10Y)  nocons bdec(3)  keep(`r(X)') adjr2


qui: reghdfe lnlag1 i.uspto##i.post_aipa granted i.uspto##i.grant18m, absorb(appln_id_us month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\tablee2.xls", ctitle(Lag1)  nocons bdec(3)  keep(`r(X)') adjr2 
qui: reghdfe lnlag3 i.uspto##i.post_aipa granted i.uspto##i.grant18m, absorb(appln_id_us month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\tablee2.xls", ctitle(Lag3)  nocons bdec(3)  keep(`r(X)') adjr2
qui: reghdfe lnlag5 i.uspto##i.post_aipa granted i.uspto##i.grant18m, absorb(appln_id_us month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\tablee2.xls", ctitle(Lag5)  nocons bdec(3)  keep(`r(X)') adjr2
qui: reghdfe lnlag7 i.uspto##i.post_aipa granted i.uspto##i.grant18m, absorb(appln_id_us month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\tablee2.xls", ctitle(Lag7)  nocons bdec(3)  keep(`r(X)') adjr2


foreach i in 50 75 90 95{
qui: reghdfe ipc7_`i' i.uspto##i.post_aipa granted i.uspto##i.grant18m, absorb(appln_id_us month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\tablee2.xls", ctitle(Sim`i')  nocons bdec(3)  keep(`r(X)') adjr2
}
rm "$DATA\tablee2.txt"
restore 






************************************************Table F1: undo Harhoff adjustments************************************************
//variables with *_nh" are citation counts and citation lags without Harhoff adjustment
use "$DATA\us_ep_twin",clear

foreach i in 3 5 7 10{
gen lnfcite_pub`i'_nh=ln(1+fcite_pub`i'_nh) 
}
qui: reghdfe lnfcite_pub3_nh i.uspto##i.post_aipa granted i.uspto##i.grant18m, absorb(appln_id_us month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\tablef1.xls", ctitle(fcite_pub3Y)  nocons bdec(3)  adjr2 keep(`r(X)') replace
qui: reghdfe lnfcite_pub5_nh i.uspto##i.post_aipa granted i.uspto##i.grant18m, absorb(appln_id_us month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\tablef1.xls", ctitle(fcite_pub5Y)  nocons bdec(3)  adjr2 keep(`r(X)')
qui: reghdfe lnfcite_pub7_nh i.uspto##i.post_aipa granted i.uspto##i.grant18m, absorb(appln_id_us month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\tablef1.xls", ctitle(fcite_pub7Y)  nocons bdec(3)  adjr2 keep(`r(X)')
qui: reghdfe lnfcite_pub10_nh i.uspto##i.post_aipa granted i.uspto##i.grant18m, absorb(appln_id_us month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\tablef1.xls", ctitle(fcite_pub10Y)  nocons bdec(3)  adjr2 keep(`r(X)')


gen lnlag1_nh=ln(lag1_nh+1)
gen lnlag3_nh=ln(lag3_nh+1)
gen lnlag5_nh=ln(lag5_nh+1)
gen lnlag7_nh=ln(lag7_nh+1)

qui: reghdfe lnlag1_nh i.uspto##i.post_aipa granted i.uspto##i.grant18m, absorb(appln_id_us month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\tablef1.xls", ctitle(Lag1)  nocons bdec(3)  adjr2 keep(`r(X)') 
qui: reghdfe lnlag3_nh i.uspto##i.post_aipa granted i.uspto##i.grant18m, absorb(appln_id_us month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\tablef1.xls", ctitle(Lag3)  nocons bdec(3)  adjr2 keep(`r(X)')
qui: reghdfe lnlag5_nh i.uspto##i.post_aipa granted i.uspto##i.grant18m, absorb(appln_id_us month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\tablef1.xls", ctitle(Lag5)  nocons bdec(3)  adjr2 keep(`r(X)')
qui: reghdfe lnlag7_nh i.uspto##i.post_aipa granted i.uspto##i.grant18m, absorb(appln_id_us month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\tablef1.xls", ctitle(Lag7)  nocons bdec(3)  adjr2 keep(`r(X)')
rm "$DATA\tablef1.txt"






************************************************Table G1: Alternate Patent Similarity Measures************************************************

use "$DATA\us_ep_twin",clear

//Panel A: Jaccard similarity based on 7-digit IPC codes
foreach i in 50 75 90 95{
reghdfe ipc7d_`i' i.uspto##i.post_aipa granted i.uspto##i.grant18m, absorb(appln_id_us month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\tableg1_panelA.xls", bdec(3) keep(`r(X)') adjr2
}
rm "$DATA\tableg1_panelA.txt"


//Panel B: Cosine similarity based on 4-digit IPC codes
foreach i in 50 75 90 95{
reghdfe ipc4_`i' i.uspto##i.post_aipa granted i.uspto##i.grant18m, absorb(appln_id_us month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\tableg1_panelB.xls", bdec(3) keep(`r(X)') adjr2
}
rm "$DATA\tableg1_panelB.txt"


//Panel C: text-based similarity
foreach i in 50 75 90 95{
reghdfe esim_`i' i.uspto##i.post_aipa granted i.uspto##i.grant18m, absorb(appln_id_us month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\tableg1_panelC.xls", bdec(5) keep(`r(X)') adjr2
}
rm "$DATA\tableg1_panelC.txt"





**********************************Figure 10 AIPA's effect on similarity at different levels of technology overlap******************************
***runing the following regressions
/*forvalues i=5(5)95{
qui: reghdfe ipc7_`i' i.uspto##i.post_aipa granted i.uspto##i.grant18m month_X_us, absorb(appln_id_us month) vce(cluster month_country)
outreg2 using "$DATA\sim2.xls", ctitle(Sim`i')  nocons bdec(3)  adjr2
}*/
***import the coefficients from the excel file"***
serrbar aipa2 se percentile, scale(1.96) ///
addplot(line aipa2 percentile, lcolor(maroon) lpattern(dash)) ///
title("The AIPA Impact on Technological Similarity", siz(medsmall)) ///
xtitle("Percentile b/w the focal patent and next-generation patents", size(medsmall)) ytitle("") ylab(,nogrid) ///
legend(region(color(white))  c(3) order(1 "95% CI" 2 "Coeff. on US X Post AIPA (d)")) ///
plotregion(fcolor(white)) graphregion(color(white)) bgcolor(white) 
graph export "$DATA\aipa_effect2.eps", replace






*****************************Figure G1: alternative similarity measures*****************************
/*Step1: running the regressions and export the coefficients into excel files
use "$DATA\us_ep_twin",clear
forvalues i=5(5)95{
qui: reghdfe ipc7_`i' i.uspto##i.post_aipa granted i.uspto##i.grant18m, absorb(appln_id_us month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\IPC7-Cosine.xls", keep(`r(X)') adjr2
}
rm "$DATA\IPC7-Cosine.txt"


forvalues i=5(5)95{
qui: reghdfe ipc7d_`i' i.uspto##i.post_aipa granted i.uspto##i.grant18m, absorb(appln_id_us month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\Jaccard.xls", keep(`r(X)') adjr2
}
rm "$DATA\Jaccard.txt"


forvalues i=5(5)95{
qui: reghdfe ipc4_`i' i.uspto##i.post_aipa granted i.uspto##i.grant18m, absorb(appln_id_us month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\IPC4-Cosine.xls", keep(`r(X)') adjr2
}
rm "$DATA\IPC4-Cosine.txt"


forvalues i=5(5)95{
reghdfe esim_`i' i.uspto##i.post_aipa granted i.uspto##i.grant18m, absorb(appln_id_us month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\Text-Cosine.xls", keep(`r(X)') adjr2
}
rm "$DATA\Text-Cosine.txt"

Step2: import the coefficients from excel files

Step3: plot the coefficients using the following codes
*/
twoway (connect ipc7cosine var1, msymbol(plus)) ///
(connect ipc4cosine var1, lpattern(dash) msymbol(diamond)) ///
(connect ja var1, lpattern(dash) msymbol(triangle)) ///
(connect text var1, lpattern(dash) msymbol(circle)), ///
plotregion(fcolor(white)) graphregion(color(white)) bgcolor(white)  ///
 legend(region(color(white)) col(2) order (1 "IPC7-Cosine" 2 "IPC4-Cosine" 3 "Jaccard" 4 "Text-Cosine") size(medsmall)) ///
  title("AIPA Impact on Different Similarity Measures", size(med)) xtitle("Percentile")  ylab(,nogrid)
  graph export "$DATA\alt_sim.eps", replace 
  